package cn.eng.org.service.impl;
import cn.eng.basic.query.BaseQuery;
import cn.eng.basic.service.impl.BaseServiceImpl;
import cn.eng.basic.utils.PageList;
import cn.eng.org.domain.Department;
import cn.eng.org.mapper.DepartmentMapper;
import cn.eng.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Cacheable( cacheNames = {"dept"})
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;


    @Override
    public List<Department> loadAll() {
        return super.loadAll();
    }

    @Override
    public PageList<Department> queryPage(BaseQuery query) {
        return super.queryPage(query);
    }

    /**
     * 添加对象
     * @param department
     */
    @Transactional
    @Override
    public void save(Department department) {
        departmentMapper.save(department);

        this.update(department);
    }

    /**
     * 删除单个
     * @param id
     */
    @Transactional
    @Override
    public void remove(Long id) {
        departmentMapper.remove(id);
    }

    /**
     * 修改对象
     * @param department
     */
    @Transactional
    @Override
    public void update(Department department) {
        //添加department 表中的DirPath字段
        StringBuilder builder = new StringBuilder();
        if (department.getParent_id()==null){
            builder.append("/"+department.getId());
        }else {
            builder.append(departmentMapper.loadById(department.getParent_id()).getDirPath()+"/"+department.getId());
        }
        department.setDirPath(builder.toString());
        departmentMapper.update(department);

    }

    /**
     * 部门树
     * @return
     */
    @Override
    public List<Department> deptTree() {
        return getDeptTree();
    }
    /**
     * 抽取部门树的方法
     * @return
     */
    private List<Department> getDeptTree() {
        //获取所有的部门列表
        List<Department> departments = departmentMapper.loadAll();
        Map<Long,Department> map = new HashMap<>();
        //遍历并装进map中
        for (Department department:departments) {
            map.put(department.getId(),department);
        }
        List<Department> deptTree = new ArrayList<>();

        for (Department department:departments) {
            //先找出顶级部门
           if (department.getParent_id()==null){
               deptTree.add(department);
           }else {
               //负责就是有父级部门的子部门,通过getParent_id找到父级部门
               Department parentDepartment = map.get(department.getParent_id());
                //然后装进父级部门的的children集合中
               if (parentDepartment!=null){
                   parentDepartment.getChildren().add(department);
               }
           }
        }
        return deptTree;
    }
}
